package com.arron.algorithm.leetcodetop100.回溯.岛屿;

public class 岛屿数量 {



    public int numIslands(char[][] grid) {

        int m = grid.length;
        int n = grid[0].length;

        int count = 0;
        for (int i = 0; i < m; i++) {

            for (int j = 0; j < n; j++) {
                //如果遇到陆地，将其岛屿全部淹没
                if(grid[i][j] == '1'){
                    dfs(grid,i,j);
                    count++;
                }
            }
        }
        return count;

    }

    private void dfs(char[][] grid, int i, int j) {

        if (i<0 || i >= grid.length || j<0 || j>= grid[0].length || grid[i][j] == '0'){
            return;
        }



        grid[i][j] = '0';

        //上边
        dfs(grid,i+1,j);
        //下边
        dfs(grid,i-1,j);
        //左边
        dfs(grid,i,j-1);
        //右边
        dfs(grid,i,j+1);
    }

}

